package cn.melon.view.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;

import cn.melon.mod.roll.RollInfo;
import cn.melon.mod.roll.RollRequest;
import cn.melon.window.MainWindow;
import cn.melon.window.RollInfoModifyWindow;

/**
 * 监听器，在学籍信息修改页面点击提交按钮后做出响应，提交修改结果给服务器
 * @ClassName: SubmitRollModification
 * @author Hzy
 */
public class SubmitRollModification implements ActionListener{

    /**
     * 学籍信息请求类
     */
    RollRequest request;
    /**
     * 修改信息窗口句柄, 通过此句柄关闭修改窗口
     */
    RollInfoModifyWindow rollInfoModifyWindow;

    /**
     * @param rollInfoModifyWindow 获取修改信息的窗口句柄, 修改信息过后句柄自动销毁
     * @param m 主窗口句柄, 通过此变量实时更新主窗口
     */
    public SubmitRollModification(RollInfoModifyWindow rollInfoModifyWindow,  MainWindow m) {
        request = new RollRequest(m);
        this.rollInfoModifyWindow = rollInfoModifyWindow;
    }
    
    @Override
    public void actionPerformed(ActionEvent e){
        JButton btn = (JButton) e.getSource();
        RollInfoModifyWindow w = (RollInfoModifyWindow) btn.getRootPane().getParent();
        RollInfo newRollInfo = new RollInfo();
//      private JTextField rollUpdateDateChange,personalMottoChange,nameChange,sexChange,nationChange,studentNumberChange,cardNumChange,politicalStatusChange,
//      martialStatusChange,idCardChange,nativePlaceChange,dateBirthChange,addressChange,curCampusChange,departmentChange,majorChange,curGradeChange,lengthChange,
//      phoneChange,emailChange;

        newRollInfo.setSign(w.getPersonalMottoChange());
        newRollInfo.setName(w.getNameChange());
        newRollInfo.setSex(w.getSexChange());
        newRollInfo.setNation(w.getNationChange());
        newRollInfo.setStudentNumber(w.getStudentNumberChange());
        newRollInfo.setCardNum(w.getCardNumChange());
        newRollInfo.setPoliticalStatus(w.getPoliticalStatusChange());
        newRollInfo.setMaritalStatus(w.getMartialStatusChange());
        newRollInfo.setIdCard(w.getIdCardChange());
        newRollInfo.setNativePlace(w.getNativePlaceChange());
        newRollInfo.setDateBirth(w.getDateBirthChange());
        newRollInfo.setAddress(w.getAddressChange());
        newRollInfo.setCurCampus(w.getCurCampusChange());
        newRollInfo.setDepartment(w.getDepartmentChange());
        newRollInfo.setMajor(w.getMajorChange());
        newRollInfo.setCurGrade(w.getCurGradeChange());
        newRollInfo.setPhone(w.getPhoneChange());
        newRollInfo.setEmail(w.getEmailChange());
        newRollInfo.setLength(w.getLengthChange());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String modifyDate = df.format(new Date());
        newRollInfo.setUpdateDate(modifyDate);
        
//        已经获得新的用户信息，现在要向服务器端发送修改请求，服务器端检测修改数据的合法性，并使用新的RollInfo更新视图
        
        request.updateInfo(newRollInfo);
        rollInfoModifyWindow.setVisible(false);
    }
}
